package com.example.demo.service;

import com.example.demo.mapper.BookMapper;
import com.example.demo.pojo.Book;
import com.example.demo.pojo.Page;
import com.example.demo.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BillService billService;

    public List<Book> getBookByCustNum(String custNum) {
        return bookMapper.getBookByCustNum(custNum);
    }

    public int updateBookByCustNum(int bookID, String custNum, int roomID, Date createTime, String arriveDate, String leaveDate, String remark) {
        return bookMapper.updateBookByBookID(bookID,custNum,roomID,createTime,arriveDate,leaveDate,remark);
    }

    public int deleteBook(String out_trade_no){
        return bookMapper.deleteBook(out_trade_no);
    }

    public List<Book> findByBook(int  custID){
        return bookMapper.findByBook(custID);
    }

    public List<Book> findAllbooktest(Date startDate,int typeID){
        return bookMapper.findAllbooktest(startDate,typeID);
    }

    public List<Book> findAllbookmonth(Date startDate,int typeID){
        return bookMapper.findAllbookmonth(startDate,typeID);
    }
    //admin界面使用

    public List<Book> pageBooksBy(String start,String end,String option,String custNum) throws Exception{
        if(custNum.equals("")){
            //为空
            if(option.equals("no")){
                if(start.equals("")){
                    if(end.equals("")){
                        //都为空不用查
                        System.out.println("1");
                        return null;
                    }else {
                        //end
                        List<Book> bookListAll = bookMapper.getBooksByE( end);
                        System.out.println("2");
                        return bookListAll;
                    }
                }else {
                    if(end.equals("")){
                        //start
                        List<Book> bookListAll = bookMapper.getBooksByS(start);
                        System.out.println("3");
                        return bookListAll;
                    }else {
                       //start end
                        List<Book> bookListAll = bookMapper.getBooksBySE(start, end);
                        System.out.println("4");
                        return bookListAll;
                    }
                }
            }else {
                if(start.equals("")){
                    if(end.equals("")){
                        //option
                        List<Book> bookListAll = bookMapper.getBooksByO(option);
                        System.out.println("5");
                        return bookListAll;
                    }else {
                        //option end
                        List<Book> bookListAll = bookMapper.getBooksByOE(end, option);
                        System.out.println("6");
                        return bookListAll;
                    }
                }else {
                    if(end.equals("")){
                        //option start
                        List<Book> bookListAll = bookMapper.getBooksBySO(start, option);
                        System.out.println("7");
                        System.out.println(bookListAll);

                        return bookListAll;
                    }else {
                        //option end start
                        List<Book> bookListAll = bookMapper.getBooksBySEO(start, end,option);
                        System.out.println("8");
                        return bookListAll;
                    }
                }
            }
        }else{
            if(option.equals("no")){
                if(start.equals("")){
                    if(end.equals("")){
                        //custNum
                        List<Book> bookListAll = bookMapper.getBooksByC(custNum);
                        System.out.println("9");
                        return bookListAll;

                    }else {
                        //custNum end
                        List<Book> bookListAll = bookMapper.getBooksByEC(end, custNum);
                        System.out.println("10");
                        return bookListAll;
                    }
                }else {
                    if(end.equals("")){
                        // custNum start
                        List<Book> bookListAll = bookMapper.getBooksBySC(start, custNum);
                        System.out.println("11");
                        return bookListAll;
                    }else {
                        // custNum start end
                        List<Book> bookListAll = bookMapper.getBooksBySEC(custNum, start ,end) ;
                        System.out.println("12");
                        return bookListAll;
                    }
                }
            }else {
                if(start.equals("")){
                    if(end.equals("")){
                        //option custNum
                        List<Book> bookListAll = bookMapper.getBooksByOC(option, custNum );
                        System.out.println("13");
                        return bookListAll;
                    }else {
                        //option end custNum
                        List<Book> bookListAll = bookMapper.getBooksByEOC(end, option ,custNum);
                        System.out.println("14");
                        return bookListAll;
                    }
                }else {
                    if(end.equals("")){
                        //option start custNum
                        List<Book> bookListAll = bookMapper.getBooksBySOC(start, custNum,option);System.out.println("15");
                        return bookListAll;
                    }else {
                        //option end start custNum
                        List<Book> bookListAll = bookMapper.getBooksBySEOC(start, end ,option,custNum);System.out.println("16");
                        return bookListAll;
                    }
                }
            }
        }
    }

    public String getShow(String start,String end,String option,String custNum){
        String show = "";
        if(custNum.equals("")){
            //为空
            if(option.equals("no")){
                if(start.equals("")){
                    if(end.equals("")){
                        //都为空不用查
                        System.out.println("1");
                        return null;
                    }else {
                        //end
                        show = "查询结果-------截止日为：" + end+"   的结果如下";
                        return show;
                    }
                }else {
                    if(end.equals("")){
                        //start
                        show = "查询结果-------起始日为：" + start+"   的结果如下";
                        return show;
                    }else {
                        //start end
                        show = "查询结果-------起始日为：" + start+" 截止日为:"+ end  + "   的结果如下";
                        return show;
                    }
                }
            }else {
                if(start.equals("")){
                    if(end.equals("")){
                        //option
                        show = "查询结果-------订单状态为：" +option  + "   的结果如下";
                        return show;
                    }else {
                        //option end
                        show = "查询结果-------订单状态为：" +option  +" 截止日为:"+ end  + "  的结果如下";
                        return show;
                    }
                }else {
                    if(end.equals("")){
                        //option start
                        show = "查询结果-------订单状态为：" +option  +" 起始日为:"+ start  + "  的结果如下";
                        return show;
                    }else {
                        //option end start
                        show = "查询结果-------订单状态为：" +option  +" 起始日为:"+ start +" 截止日为:"+ end  + "  的结果如下";
                        return show;
                    }
                }
            }
        }else{
            if(option.equals("no")){
                if(start.equals("")){
                    if(end.equals("")){
                        //custNum
                        show = "查询结果-------客户证件号为：" +custNum    + "  的结果如下";
                        return show;
                    }else {
                        //custNum end
                        show = "查询结果-------客户证件号为：" +custNum  +" 截止日为:"+ end   + "  的结果如下";
                        return show;
                    }
                }else {
                    if(end.equals("")){
                        // custNum start
                        show = "查询结果-------客户证件号为：" +custNum   +" 起始日为:"+ start  + "  的结果如下";
                        return show;
                    }else {
                        // custNum start end
                        show = "查询结果-------客户证件号为：" +custNum  +" 起始日为:"+ start +" 截止日为:"+ end  + "  的结果如下";
                        return show;
                    }
                }
            }else {
                if(start.equals("")){
                    if(end.equals("")){
                        //option custNum
                        show = "查询结果-------客户证件号为：" +custNum   +" 订单状态为：" +option   + "  的结果如下";
                        return show;
                    }else {
                        //option end custNum
                        show = "查询结果-------客户证件号为：" +custNum   +" 订单状态为：" +option +" 截止日为:"+ end   + "  的结果如下";
                        return show;
                    }
                }else {
                    if(end.equals("")){
                        //option start custNum
                        show = "查询结果-------客户证件号为：" +custNum   +" 订单状态为：" +option  +" 起始日为:"+ start   + "  的结果如下";
                        return show;
                    }else {
                        //option end start custNum
                        show = "查询结果-------客户证件号为：" +custNum   +" 订单状态为：" +option  +" 起始日为:"+ start +" 截止日为:"+ end   + "  的结果如下";
                        return show;
                    }
                }
            }
        }
    }

    public Page pageBooks(String currentPage) throws Exception{
        int allRowsAmount =bookMapper.getAllRowsAmount();

        List<Book> bookListAll   = bookMapper.getBookAll_weicuoli() ;
        List<Book> bookListAllyichuli = bookMapper.getBookAll_yichuli();
        bookListAll.addAll(bookListAllyichuli);

        PageUtil pageUtil =new PageUtil();
        pageUtil.setAllRowsAmount(allRowsAmount);
        if(currentPage !=null){
            pageUtil.setCurrentPage(Integer.parseInt(currentPage));
        }
        pageUtil.calculatePage();

        ArrayList<Book> showBooks = new ArrayList<Book>();
        int CurrentPageStartRow = pageUtil.getCurrentPageStartRow()-1;
         if(allRowsAmount < (CurrentPageStartRow + pageUtil.getPageSize()) ){
            //总数比访问页面数小
            for(int i = CurrentPageStartRow ; i< allRowsAmount ; i++){
                if(bookListAll.get(i) != null) {
                    showBooks.add(bookListAll.get(i));
                }
            }
        }else{
            for(int i =0;i<pageUtil.getPageSize();i++){
                if(bookListAll.get(CurrentPageStartRow + i) != null) {
                    showBooks.add(bookListAll.get(CurrentPageStartRow + i));
                }
            }
        }

        Page page =new Page();
        page.setPrevPage(pageUtil.getPrevPage());
        page.setNextPage(pageUtil.getNextPage());
        page.setShowBooks(showBooks);
        page.setShowPageNums(pageUtil.getShowPageNums());
        page.setCurrentPage(pageUtil.getCurrentPage());
        return page;
    }

    public int updateBookByBookID1(int bookID, String custNum, int roomID, Date createTime, String arriveDate,
                                  String leaveDate, String remark,String bookState) {
        return bookMapper.updateBookByBookID1(bookID,custNum,roomID,createTime,arriveDate,leaveDate,remark,bookState);
    }

    public Book getBookByBookID(int bookID) {
        return bookMapper.getBookByBookID(bookID);
    }

    public int deleteBookByBookId(int bookID) {
        //实现级联删除，删除book表就删除对应的bill
        billService.deleteBillByBookId(bookID);
        return bookMapper.deleteBookByBookId(bookID);
    }

    //-----------------------------------

    public int addBook(String custNum, int roomID, Date bookTime, Date bookCheckinTime, int bookDay, int bookState) {
        return bookMapper.addBook(custNum,roomID,bookTime,bookCheckinTime,bookDay,bookState);
    }



    public int addCustBook(Book book){
        return bookMapper.addCustBook(book);
    }

    public List<Book> findByAll(int custID){
        return bookMapper.findByAll(custID);
    }

    public int updateBookStateByBookId(int bookState,int bookID) {
        return bookMapper.updateBookStateByBookId(bookState,bookID);
    }

    public String getBookEmail(String bookID) {
        return bookMapper.getBookEmail(bookID);
    }

    public String getBookCustName(String bookID) {
        return bookMapper.getBookCustName(bookID);
    }

    public int[] getRoomIDbyBookID(String bookID) {
        return bookMapper.getRoomIDbyBookID(bookID);
    }

    public void updateRoomIDByBookID(int roomID, String bookID) {
        bookMapper.updateRoomIDByBookID(roomID,bookID);
    }


    public int getcustIDByBookID(Integer bookID) {
        return bookMapper.getcustIDByBookID(bookID);
    }
}
